import { href } from 'react-router';

import type { GitCredentials } from '~/models/git-repository';
import { createFetcherSubmitHook } from '~/utils/router';

import type { Route } from './+types/git.update';

interface UpdateGitRepoData {
  author: {
    email: string;
    name: string;
  };
  credentials: GitCredentials;
  uri: string;
  workspaceId?: string;
  projectId: string;
}

export async function clientAction({ request }: Route.ClientActionArgs) {
  const data = (await request.json()) as UpdateGitRepoData;

  return window.main.git.updateGitRepo(data);
}

export const useGitProjectUpdateActionFetcher = createFetcherSubmitHook(
  submit => (data: UpdateGitRepoData) => {
    return submit(JSON.stringify(data), {
      method: 'POST',
      action: href(`/git/update`),
      encType: 'application/json',
    });
  },
  clientAction,
);
